package com.easybacklog.client.webservices.github;

import com.easybacklog.client.EasyBacklogGlobalVariables;
import com.easybacklog.client.model.Project;
import com.easybacklog.client.model.ProjectFeature;
import com.easybacklog.client.webservices.ConvertJsonp;
import com.easybacklog.client.webservices.ConvertJsonpToFeatureFile;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.jsonp.client.JsonpRequestBuilder;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class RequestGithubFileContent {

	// http://livesourceagile.com:8080/LiveSourceWebServices/GetGithubClassDocumentService
	// ?projectName=TravelLog
	// &username=allineo
	// &sha=c2278fdc2f83aad7943a350d3d37ad7dd6d0ef63
	// &filePath=src/com/amazon/aws/samplecode/travellog/entity/Comment.java

	private static String url = "http://livesourceagile.com:8080/LiveSourceWebServices/GetGithubClassDocumentService";

	public static void getJsonpRequest(Project project,
			final ProjectFeature file) {

		JsonpRequestBuilder jsonp = new JsonpRequestBuilder();

		jsonp.setTimeout(15000);

		String urlGet = url + "?projectName=";
		// + project.getGithubProjectName()
		// + "&filePath=" + file.toString() + "&username="
		// + project.getGithubUserName() + "&sha=" + file.getSha();

		jsonp.requestObject(urlGet, new AsyncCallback<JavaScriptObject>() {

			public void onFailure(Throwable throwable) {

				System.out.println("Error: " + throwable);
			}

			public void onSuccess(JavaScriptObject feed) {

				JSONObject entry = new JSONObject(feed);

				ProjectFeature feature = EasyBacklogGlobalVariables.projectFeatures
						.get(file.toString());

				feature.setFileContent(ConvertJsonp.convertToString(entry
						.get("fileContent")));

				feature = ConvertJsonpToFeatureFile.convert(feature, entry);

				EasyBacklogGlobalVariables.projectFeatures.put(file.toString(),
						feature);
			}
		});
	}
}
